
package com.scs.application.modules.settlement.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.scs.application.core.model.CommonResult;
import com.scs.application.core.model.PageResult;
import com.scs.application.core.model.RestResult;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.mvc.controller.ImAndExAbleController;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.design.service.QueryGenerator;
import com.scs.application.modules.settlement.entity.DeptOprtUse;
import com.scs.application.modules.settlement.entity.DeptOprtUseItem;
import com.scs.application.modules.settlement.mapper.DeptOprtUseItemMapper;
import com.scs.application.modules.settlement.request.DeptOprtUseItemQueryRequest;
import com.scs.application.modules.settlement.request.SumHighDtlQueryRequest;
import com.scs.application.modules.settlement.service.DeptOprtUseItemService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 病人消耗子表 controller
 */
@AllArgsConstructor
@RestController
@RequestMapping("/settlement/dept_oprt_use_items")
public class DeptOprtUseItemController extends CrudController<DeptOprtUseItem, String, DeptOprtUseItemService> implements ImAndExAbleController<DeptOprtUseItemQueryRequest> {
    private final DeptOprtUseItemMapper deptOprtUseItemMapper;

    @GetMapping("/page")
    public PageResult page(DeptOprtUseItemQueryRequest request) {
        IPage page = service.page(request);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    @GetMapping("/list")
    public CommonResult list(DeptOprtUseItemQueryRequest request) {
        List<DeptOprtUseItem> list = service.list(request);
        return CommonResult.ok("查询成功", list);
    }


    @PutMapping("/batch_save")
    public CommonResult batchSave(@RequestBody @Valid DeptOprtUseItem[] children) {
        if (children == null || children.length == 0) {
            return CommonResult.error("保存失败");
        }
        boolean bSuccess = service.saveOrUpdateBatch(Lists.newArrayList(children));
        return bSuccess ? CommonResult.ok("保存成功") : CommonResult.error("保存失败");
    }


    /***
     * @param request
     * @Description:  高值耗材消耗汇总
     */
    @GetMapping("/pageSumDtl")
    public PageResult pageSumDtl(HttpServletRequest request, DeptOprtUse entity) {
        IPage<SumHighDtlQueryRequest> page = QueryGenerator.parsePage(request);
        QueryWrapper<SumHighDtlQueryRequest> queryWrapper = QueryGenerator.initQueryWrapper(request);
        fillQueryAuth(queryWrapper,new SumHighDtlQueryRequest());
        IPage pageResult = deptOprtUseItemMapper.pageSumDtl(page, queryWrapper);
        return PageResult.ok("查询成功", pageResult.getRecords(), pageResult.getTotal());
    }



    //顶部合计
    @GetMapping("/sumTopSumDtl")
    public RestResult sumTopSumDtl(String sumFields,HttpServletRequest request,DeptOprtUseItem entity) {
        if (StringUtils.isBlank(sumFields)) {
            return RestResult.ok("查询成功", null);
        }
        QueryWrapper<SumHighDtlQueryRequest> queryWrapper = QueryGenerator.initQueryWrapper(request);
        fillQueryAuth(queryWrapper,new SumHighDtlQueryRequest());
        List list = deptOprtUseItemMapper.pageSumList(queryWrapper);
        if (list == null || list.size() < 1) {
            return RestResult.ok("查询成功", null);
        }
        return RestResult.ok("查询成功", getMapNum(sumFields,list));
    }
}
